package com.tang.task;

import com.tang.pojo.TOrderInfo;
import com.tang.pojo.TRefundInfo;
import com.tang.service.TOrderInfoService;
import com.tang.service.TRefundInfoService;
import com.tang.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class WxPayTask {

    @Resource
    private TOrderInfoService orderInfoService;
    @Resource
    private WxPayService wxPayService;
    @Resource
    private TRefundInfoService refundInfoService;

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm() throws Exception {
        log.info("orderConfirm 被执行......");
        List<TOrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5);
        for (TOrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单 ===> {}", orderNo);
            //核实订单状态：调用微信支付查单接口
            wxPayService.checkOrderStatus(orderNo);
        }
    }

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未成功的退款单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void refundConfirm() throws Exception {
        log.info("refundConfirm 被执行......");
//找出申请退款超过5分钟并且未成功的退款单
        List<TRefundInfo> refundInfoList =
                refundInfoService.getNoRefundOrderByDuration(5);
        for (TRefundInfo refundInfo : refundInfoList) {
            String refundNo = refundInfo.getRefundNo();
            log.warn("超时未退款的退款单号 ===> {}", refundNo);
//核实订单状态：调用微信支付查询退款接口
            wxPayService.checkRefundStatus(refundNo);
        }
    }

}
